from fastapi import APIRouter
from models.models import Hotel, HotelImage
from models.room_type import RoomType

hotel_router = APIRouter()

@hotel_router.get("/hotel/{hotel_id}")
async def get_hotel_detail(hotel_id: str):
    """
    获取酒店详细信息
    
    Args:
        hotel_id: 酒店ID
    
    Returns:
        酒店详细信息，包括房型列表
    """
    try:
        # 获取酒店基本信息
        hotel = await Hotel.get_or_none(id=hotel_id)
        if not hotel:
            return {"code": 404, "message": "酒店未找到", "data": {}}
        
        # 获取酒店图片
        images = []
        hotel_images = await hotel.images.all()
        for image in hotel_images:
            images.append({
                "id": image.id,
                "image_url": image.image_url,
                "is_main": image.is_main,
            })
        
        # 获取房型信息
        room_types = await RoomType.filter(hotel_id=hotel_id).all()
        
        # 将房型信息转换为字典格式
        room_types_data = []
        for room_type in room_types:
            room_types_data.append({
                "id": room_type.id,
                "name": room_type.name,
                "area": room_type.area,
                "beds": room_type.beds,
                "capacity": room_type.capacity,
                "features": room_type.features,
                "price": room_type.price,
                "original_price": room_type.original_price,
                "discount": room_type.discount,
                "description": room_type.description,
                "images": room_type.images,
            })
        
        return {
            "code": 200, 
            "message": "ok", 
            "data": {
                "hotel": {
                    "id": hotel.id,
                    "hotel_name": hotel.hotel_name,
                    "city": hotel.city,
                    "address": hotel.address,
                    "price": hotel.price,
                    "stock": hotel.stock,
                    "facilities": hotel.facilities,
                    "status": hotel.status,
                    "images": images
                },
                "room_types": room_types_data
            }
        }
    except Exception as e:
        return {
            "code": 500, 
            "message": f"获取酒店详情失败: {str(e)}", 
            "data": {}
        }